<?php
/**
 * 《装箱单》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/ExpressUtil.class.php');

class Packing extends ExpressUtil {
	public $name = '装箱单';
	public $tbl = Constant::tbl_express_packing;
			
	public $type = array(
		'-' => '请选择',
		'1' => '货款',		
		'2' => '业务费',
		
		'3' => '固定资产',
		'4' => '行政',
	
		'5' => '餐补',
		'6' => '交通费',
		'7' => '食堂',	
		'8' => '奖金',

		'9' => '其它',
	);
	
	public $invoice = array(
		'-' => '请选择',
		'1' => '收据',
		'2' => '发票',
				
		'9' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '报销申请',
		'2' => '等待报销',
		'3' => '正在报销',
		'4' => '等待领钱',
		'5' => '完成报销',
		// '6' => '已经做帐',
		'6' => '取消',

		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		
		'emp' => '报销人',
		'date' => '报销日期',	
		'finish' => '完成时间',
	
		'payment' => '支付',
		'total' => '金额',
		'status' => '状态',

		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'emp' => '报销人',
		'invoice' => '凭证',
		'payment' => '支付',
		'currency' => '币种',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('type_json', json_encode($this->type));
		
		$controller->assign_by_ref('invoice_option', $this->invoice);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		// $controller->assign('t1', strtotime('-2 month'));

		$controller->display($request, 'finance/expense/find');
		return true;
	}
	
	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'date') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['date'];
		}
		
		if ($px == 'finish') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['finish'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {		
		$name_s = $request['name_s'];
		
		$invoice_s = $request['invoice_s'];
		$payment_s = $request['payment_s'];
				
		$amount1_s = $request['amount1_s'];
		$amount2_s = $request['amount2_s'];
		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
				
		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR content LIKE '%" . $name_s . "%')";
		}
		
		if (!empty($invoice_s) && $invoice_s <> '-') {
			$where .= " AND invoice = '" . $invoice_s . "'";
		}
		
		if (!empty($payment_s) && $payment_s <> '-') {
			$where .= " AND payment = '" . $payment_s . "'";
		}
		
		if (!empty($amount1_s)) {
			$where .= " AND amount >= '" . $amount1_s . "'";
		}

		if (!empty($amount2_s)) {
			$where .= " AND amount < '" . $amount2_s . "'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND finish >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND finish < '" . $date2_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		$emp = $_SESSION['id'];
		
		$p = array('1'=>'166', '2'=>'176', '3'=>'177');
		
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
		
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}

		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['num_'] = $this->_num($row['id']);
				$row['borrow'] = $this->_borrow($row['emp'], $row['status']);
				
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		} else {
			$controller->clear_assign('result');
		}
				
		$controller->display($request, 'finance/expense/list');
		return true;
	}
	
	function _batch_insql($request) {		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		return $insql;
	}
	
	function _num($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE expense = '%s'";
		$sql = sprintf($fmt, Constant::tbl_cw_expense_log, $id);
		// debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}
	
	function _borrow($emp, $status) {
		if ($status == '5') {
			return '';
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND status = '3'";
		$sql = sprintf($fmt, Constant::tbl_cw_borrow, $emp);
		// debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return '有';
		}

		return '无';
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-3 day'));
		
		$row['item'] = json_encode($this->_item_blank(2));		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/expense/add');
		return true;
	}
	
	function _item_blank($len = 5) {
		$ret = array();
		$i = 0;
		for ($i = 0; $i < $len; $i++) {
			$row['date'] = date("Y-m-d");
			$row['type'] = "9:其它";
			$row['amount'] = "0.00";
			
			$row['ok'] = true;
			
			$ret[] = $row;
		}
		
		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		
		$name = addslashes($request['name']);
		$content = addslashes($request['content']);
		
		$total = $request['total'];
		$currency = $request['currency'];
		$rate = $request['rate'];
		$rmb = $request['rmb'];
		
		$borrow = $request['borrow'];
		$amount = $total - $borrow;
		
		$invoice = $request['invoice'];
		$payment = $request['payment'];
		$payment_x = addslashes($request['payment_x']);
		$this->_update_bank("1", $emp, $request['payment_x']);
		
		$date = $request['date'];		
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, name, content, currency, rate, rmb, total, borrow, amount, invoice, payment, payment_x, date, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $name, $content, $currency, $rate, $rmb, $total, $borrow, $amount, $invoice, $payment, $payment_x, $date, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$id = $db->insert_id();
		
		$this->_insert_expense_log($id, $request['log'], $emp);

		$request['id_s'] = $id;

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _insert_expense_log($id, $log, $emp) {
		require_once(dirname(__FILE__) . '/ExpenseLog.class.php');
		$aa = new ExpenseLog();
		
		return $aa->_insert($id, $log, $emp);
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['log'] = json_encode($this->_expense_log($row['id']));

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/expense/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['log'] = json_encode($this->_expense_log($row['id']));

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/expense/edit');
		return true;
	}
	
	function _expense_log($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/ExpenseLog.class.php');
		$aa = new ExpenseLog();
		
		return $aa->_list($id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		
		$name = addslashes($request['name']);
		$content = addslashes($request['content']);
		
		$total = $request['total'];		
		$currency = $request['currency'];
		$rate = $request['rate'];
		$rmb = $request['rmb'];
		
		$borrow = $request['borrow'];
		$amount = $total - $borrow;
		
		$invoice = $request['invoice'];
		$payment = $request['payment'];
		$payment_x = addslashes($request['payment_x']);		
		$this->_update_bank("1", $emp, $request['payment_x']);		
		
		$date = $request['date'];
				
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', name = '%s', content = '%s', currency = '%s', rate = '%s', rmb = '%s', total = '%s', borrow = '%s', amount = '%s', ";
		$fmt .= "invoice = '%s', payment = '%s', payment_x = '%s', date = '%s', emp = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $name, $content, $currency, $rate, $rmb, $total, $borrow, $amount, $invoice, $payment, $payment_x, $date, $emp, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}		
		
		$this->_update_expense_log($id, $request['log'], $emp);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _update_expense_log($id, $log, $emp) {
		require_once(dirname(__FILE__) . '/ExpenseLog.class.php');
		$aa = new ExpenseLog();
		
		return $aa->_update($id, $log, $emp);
	} 

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->invoice = $this->_shift($this->invoice);
		$this->payment = $this->_shift($this->payment);
		
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['amount_'] = $row['amount'] * -1; 
		$row['log'] = $this->_expense_log($row['id']);
				
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/expense/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		} 
		
		$row['type_t'] = $this->type[$row['type']];
		
		$row['invoice_t'] = $this->invoice[$row['invoice']];
		$row['payment_t'] = $this->payment[$row['payment']];
	
		$row['payment_x'] = json_decode($row['payment_x']);
		if ($row['payment'] == '2') {
			$row['payment_x']->code = $this->_format($row['payment_x']->code, 4, ' '); 
		}
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
			
			require_once(dirname(__FILE__) . '/ExpenseLog.class.php');
			$aa = new ExpenseLog();
			$aa->_delete($id);
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('149') || $this->_audit('150')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
		
	function _ss_status($id, $status) {
		$this->_status($id, $status);
	}
	
	function tj1($controller, $request) {
		$a = array(
			'南山报销'=>array('a'=>'322', 'office'=>'1'),
			'沙井报销'=>array('a'=>'323', 'office'=>'2'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
						
			$this->tj($controller, $request);
		}
	}
	
	function tj2($controller, $request) {
		$a = array(
			'南山报销（周报）'=>array('a'=>'322', 'office'=>'1'),
			'沙井报销（周报）'=>array('a'=>'323', 'office'=>'2'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
			
			$this->tjw($controller, $request);
		}				
	}
	
	function tj($controller, $request) {		
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = $request['title'];		
		$office = $request['office'];
		
		$d1 = $this->_tj_df("finish", "%Y-%m-%d");
		$d2 = $this->_tj_df("createtime", "%Y-%m-%d");
		$d3 = $this->_tj_df("modifytime", "%Y-%m-%d");
		
		$dd = $this->_tj_df_not("%Y-%m-%d");
		
		$cc = array(
			'完成报销'=>array('w' => "office = '$office' AND $d1", 'b'=>'currency, status'),	
			'新增信息'=>array('w' => "office = '$office' AND $d2", 'b'=>'currency, status'),
			'更新信息'=>array('w' => "office = '$office' AND $d3 AND $dd", 'b'=>'currency, status'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}
		
		$controller->clear_assign('result');

		$body = $this->_tj_html($controller, $request, 'finance/expense/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
		
	function tjw($controller, $request) {		
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$this->pretreat($controller);

		$from = $this->email;
		$subject = $request['title'];		
		$office = $request['office'];
		
		$d1 = $this->_tj_df("finish", "%Y-%V");
		$d2 = $this->_tj_df("createtime", "%Y-%V");
		$d3 = $this->_tj_df("modifytime", "%Y-%V");
		
		$dd = $this->_tj_df_not("%Y-%m-%d");
		
		$cc = array(
			'完成报销'=>array('w' => "office = '$office' AND $d1", 'b'=>'currency, status'),	
			'新增信息'=>array('w' => "office = '$office' AND $d2", 'b'=>'currency, status'),
			'更新信息'=>array('w' => "office = '$office' AND $d3 AND $dd", 'b'=>'currency, status'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "WHERE office = '$office'")
		);
		
		$this->_tj_bb($controller, $aa);
			
		$body = $this->_tj_html($controller, $request, 'finance/expense/tj_list');
				
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
		
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'currency';
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, SUM(total*rate) AS c, SUM(total) AS d FROM %s a %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, SUM(total*rate) AS c, SUM(total) AS d FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}
	
	function status($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);
		
		$this->_status($id, $status);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _status($id, $status) {
		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE expense = '%s'";
		$sql = sprintf($fmt, Constant::tbl_cw_expense_log, $status, $id);
		$db->query($sql);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_code($controller, $request) {

		$data = $this->_ajax_code($request);
		echo json_encode($data);
	}

	function _ajax_code($request) {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND code LIKE '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		// debug($sql);
		$result = $db->query($sql);

		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['emp_t'] = Name::emp($row['emp']);

			$ret[] = $row;
		}

		return $ret;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$num = array(
			'1' => '按次数',
			'2' => '按金额',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'finance/expense/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(date, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(date, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(date, '%Y-%m')";
		}

		return $px;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(total) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(total) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} elseif ($px == 'invoice') {
			$row['a'] = $this->invoice[$row['a']];
		} elseif ($px == 'payment') {
			$row['a'] = $this->payment[$row['a']];
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}
	
	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE payment = '2' AND payment_x is NULL";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$payment_x = $this->_payment_x($row['emp']);			
			
			$fmt = "UPDATE %s SET payment_x = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $payment_x, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function _tj_a($controller, $request) {	
		$s = $this->groupby($controller, $this->groupby_s);
		$df = "DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')";

		$fmt = "SELECT %s as a, SUM(amount) as c FROM %s a WHERE date >= %s GROUP BY %s ORDER BY c DESC";
		$sql = sprintf($fmt, $s, $this->tbl, $df, $s);
		echo $sql . "\n";

		$this->_group_tj($controller, $sql);

		return $controller->sunxy($request, 'tj_a');
	}	
}
?>